package com.lovo.frame;

import com.lovo.service.UserService;
import com.lovo.service.impl.UserServiceImpl;
import com.lovo.util.JdbcUtil;
import com.lovo.util.Util;
import com.lovo.pojo.UesrDO;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Withdrawal extends JPanel implements ActionListener {
    private Atm frame;
    private JTextField teller = new JTextField();


    private Icon conImage = Util.getIcon("/img/buttonSure.jpg");
    private Icon cancelImage = Util.getIcon("/img/buttonOff.jpg");
    private JButton confirmButton;
    private JButton cancelButton;

    public Withdrawal(Atm frame) {
        this.frame = frame;
        this.setLayout(null);

        JLabel tellerText = new JLabel("请输入你要取款的金额：");
        tellerText.setBounds(314, 338, 160, 40);
        this.add(tellerText);
        //输入取款金额
        teller.setBounds(454, 338, 150, 40);
        teller.getText();
        this.add(teller);

        //确认按钮
        confirmButton = new JButton(conImage);
        confirmButton.setBounds(354, 400, 140, 50);
        confirmButton.addActionListener(this);
        this.add(confirmButton);
        //返回按钮
        cancelButton = new JButton(cancelImage);
        cancelButton.setBounds(540, 400, 140, 50);
        this.add(cancelButton);
        cancelButton.addActionListener(this);

        this.setOpaque(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == confirmButton) {
            int tellerMoney = Integer.parseInt(teller.getText());
            UserService userService = new UserServiceImpl();
            try {
                userService.getMoney(frame.getUesrDO().getId(), tellerMoney);
            } catch (Exception exception) {
                JOptionPane.showMessageDialog(this,exception);
            }
            frame.switchPanel("menu");
        } else if (e.getSource() == cancelButton) {
            frame.switchPanel("menu");
        }

    }
}


